package com.dianping.cat.consumer.state.model.transform;
import com.dianping.cat.consumer.state.model.IVisitor;
import com.dianping.cat.consumer.state.model.entity.Detail;
import com.dianping.cat.consumer.state.model.entity.Machine;
import com.dianping.cat.consumer.state.model.entity.Message;
import com.dianping.cat.consumer.state.model.entity.ProcessDomain;
import com.dianping.cat.consumer.state.model.entity.StateReport;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  17 */     this.m_linker = new DefaultLinker(true);




/*  22 */     this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

/*  26 */   public static StateReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static StateReport parse(InputStream in) {
/*  30 */     DefaultNativeParser parser = new DefaultNativeParser(in);
    StateReport stateReport = new StateReport();

    try {
/*  34 */       stateReport.accept(parser);
/*  35 */     } catch (RuntimeException e) {
      if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  39 */         throw e;
      }
    }

/*  43 */     parser.m_linker.finish();
/*  44 */     return stateReport;
  }



  public void visitDetail(Detail detail) {
    byte tag;
/*  51 */     while ((tag = readTag()) != -1) {
/*  52 */       visitDetailChildren(detail, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDetailChildren(Detail detail, int _field, int _type) {
/*  57 */     switch (_field) {
      case 1:
/*  59 */         detail.setId(Long.valueOf(readLong()));
        break;
      case 2:
/*  62 */         detail.setTotal(readLong());
        break;
      case 3:
/*  65 */         detail.setSize(readDouble());
        break;
      case 4:
/*  68 */         detail.setTotalLoss(readLong());
        break;
    }
  }



  public void visitMachine(Machine machine) {
    byte tag;
/*  77 */     while ((tag = readTag()) != -1) {
      visitMachineChildren(machine, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMachineChildren(Machine machine, int _field, int _type) {
/*  83 */     switch (_field) {
      case 1:
/*  85 */         machine.setIp(readString());
        break;
      case 2:
/*  88 */         machine.setTotal(readLong());
        break;
      case 3:
/*  91 */         machine.setTotalLoss(readLong());
        break;
      case 4:
/*  94 */         machine.setMaxTps(readDouble());
        break;
      case 5:
/*  97 */         machine.setAvgTps(readDouble());
        break;
      case 6:
/* 100 */         machine.setBlockTotal(readLong());
        break;
      case 7:
/* 103 */         machine.setBlockLoss(readLong());
        break;
      case 8:
/* 106 */         machine.setBlockTime(readLong());
        break;
      case 9:
/* 109 */         machine.setPigeonTimeError(readLong());
        break;
      case 10:
/* 112 */         machine.setNetworkTimeError(readLong());
        break;
      case 11:
/* 115 */         machine.setDump(readLong());
        break;
      case 12:
/* 118 */         machine.setDumpLoss(readLong());
        break;
      case 13:
/* 121 */         machine.setSize(readDouble());
        break;
      case 14:
/* 124 */         machine.setDelaySum(readDouble());
        break;
      case 15:
/* 127 */         machine.setDelayAvg(readDouble());
        break;
      case 16:
/* 130 */         machine.setDelayCount(readInt());
        break;
      case 33:
/* 133 */         if (_type == 1) {
/* 134 */           ProcessDomain processDomain = new ProcessDomain();

/* 136 */           visitProcessDomain(processDomain);
/* 137 */           this.m_linker.onProcessDomain(machine, processDomain); break;
/* 138 */         }  if (_type == 2) {
/* 139 */           for (int i = readInt(); i > 0; i--) {
/* 140 */             ProcessDomain processDomain = new ProcessDomain();

/* 142 */             visitProcessDomain(processDomain);
/* 143 */             this.m_linker.onProcessDomain(machine, processDomain);
          }
        }
        break;
      case 34:
/* 148 */         if (_type == 1) {
/* 149 */           Message message = new Message();

/* 151 */           visitMessage(message);
/* 152 */           this.m_linker.onMessage(machine, message); break;
/* 153 */         }  if (_type == 2) {
/* 154 */           for (int i = readInt(); i > 0; i--) {
/* 155 */             Message message = new Message();

/* 157 */             visitMessage(message);
/* 158 */             this.m_linker.onMessage(machine, message);
          }
        }
        break;
    }
  }



  public void visitMessage(Message message) {
    byte tag;
/* 169 */     while ((tag = readTag()) != -1) {
/* 170 */       visitMessageChildren(message, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMessageChildren(Message message, int _field, int _type) {
/* 175 */     switch (_field) {
      case 1:
/* 177 */         message.setId(Long.valueOf(readLong()));
        break;
      case 2:
/* 180 */         message.setTime(readDate());
        break;
      case 3:
/* 183 */         message.setTotal(readLong());
        break;
      case 4:
/* 186 */         message.setTotalLoss(readLong());
        break;
      case 5:
/* 189 */         message.setDump(readLong());
        break;
      case 6:
/* 192 */         message.setDumpLoss(readLong());
        break;
      case 7:
/* 195 */         message.setSize(readDouble());
        break;
      case 8:
/* 198 */         message.setDelaySum(readDouble());
        break;
      case 9:
/* 201 */         message.setDelayCount(readInt());
        break;
      case 10:
/* 204 */         message.setPigeonTimeError(readLong());
        break;
      case 11:
/* 207 */         message.setNetworkTimeError(readLong());
        break;
      case 12:
/* 210 */         message.setBlockTotal(readLong());
        break;
      case 13:
/* 213 */         message.setBlockLoss(readLong());
        break;
      case 14:
/* 216 */         message.setBlockTime(readLong());
        break;
    }
  }



  public void visitProcessDomain(ProcessDomain processDomain) {
    byte tag;
/* 225 */     while ((tag = readTag()) != -1) {
/* 226 */       visitProcessDomainChildren(processDomain, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitProcessDomainChildren(ProcessDomain processDomain, int _field, int _type) {
/* 231 */     switch (_field) {
      case 1:
/* 233 */         processDomain.setName(readString());
        break;
      case 2:
/* 236 */         if (_type == 1) {
/* 237 */           processDomain.addIp(readString()); break;
/* 238 */         }  if (_type == 2) {
/* 239 */           for (int i = readInt(); i > 0; i--) {
/* 240 */             processDomain.addIp(readString());
          }
        }
        break;
      case 3:
/* 245 */         processDomain.setTotal(readLong());
        break;
      case 4:
/* 248 */         processDomain.setTotalLoss(readLong());
        break;
      case 5:
/* 251 */         processDomain.setSize(readDouble());
        break;
      case 6:
/* 254 */         processDomain.setAvg(readDouble());
        break;
      case 33:
/* 257 */         if (_type == 1) {
/* 258 */           Detail detail = new Detail();

/* 260 */           visitDetail(detail);
/* 261 */           this.m_linker.onDetail(processDomain, detail); break;
/* 262 */         }  if (_type == 2) {
/* 263 */           for (int i = readInt(); i > 0; i--) {
/* 264 */             Detail detail = new Detail();

/* 266 */             visitDetail(detail);
/* 267 */             this.m_linker.onDetail(processDomain, detail);
          }
        }
        break;
    }
  }



  public void visitStateReport(StateReport stateReport) {
    byte tag;
/* 278 */     if ((tag = readTag()) != -4) {
/* 279 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/* 282 */     while ((tag = readTag()) != -1) {
/* 283 */       visitStateReportChildren(stateReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitStateReportChildren(StateReport stateReport, int _field, int _type) {
/* 288 */     switch (_field) {
      case 1:
/* 290 */         stateReport.setDomain(readString());
        break;
      case 2:
/* 293 */         stateReport.setStartTime(readDate());
        break;
      case 3:
/* 296 */         stateReport.setEndTime(readDate());
        break;
      case 33:
/* 299 */         if (_type == 1) {
/* 300 */           Machine machine = new Machine();

/* 302 */           visitMachine(machine);
/* 303 */           this.m_linker.onMachine(stateReport, machine); break;
/* 304 */         }  if (_type == 2) {
/* 305 */           for (int i = readInt(); i > 0; i--) {
/* 306 */             Machine machine = new Machine();

/* 308 */             visitMachine(machine);
/* 309 */             this.m_linker.onMachine(stateReport, machine);
          }
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 318 */       return new Date(readVarint(64));
/* 319 */     } catch (IOException e) {
/* 320 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 326 */       return Double.longBitsToDouble(readVarint(64));
/* 327 */     } catch (IOException e) {
/* 328 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 334 */       return (int)readVarint(32);
/* 335 */     } catch (IOException e) {
/* 336 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 342 */       return readVarint(64);
/* 343 */     } catch (IOException e) {
/* 344 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 350 */       return this.m_in.readUTF();
/* 351 */     } catch (IOException e) {
/* 352 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 358 */       return this.m_in.readByte();
/* 359 */     } catch (IOException e) {
/* 360 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 365 */     int shift = 0;
/* 366 */     long result = 0L;

/* 368 */     while (shift < length) {
/* 369 */       byte b = this.m_in.readByte();
/* 370 */       result |= (b & 0x7F) << shift;
/* 371 */       if ((b & 0x80) == 0) {
/* 372 */         return result;
      }
/* 374 */       shift += 7;
    }

/* 377 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/state/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */